<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of huawei
 *
 * @author sunday add 2014.5.28  | modify by dragon.hu @2015.12.4
 */
require_once 'BaseChannel.php';

//华为
class Huawei implements BaseChannel {
	
function __construct() {
        //echo "construct";
    }

    //put your code here
    function android($data) {
    	//$UID = "100008871";
        $paymentKey = "";        
        $ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
    }

    function ios($data) {
    	//$UID = "100008871";
        $paymentKey = "";        
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
    }

	function paymentCallBack($paymentKey, $platform)
	{	
		//以下代码部分来自华为demo.php,dragon.hu add @2015.12.4
		$orderLog = new ChargeOrder();
		//$orderLog->log("huawei", $_POST);
		
		$oriContent = file_get_contents('php://input');
		$orderLog->log("huawei", $oriContent);
		if (null === $oriContent || "" === $oriContent)
		{
			echo "{\"result\":1}";
			return;
		}
		//print_r($oriContent);
		//echo "<br>AAAAA<br>";
		$elements = explode('&', $oriContent);
		//print_r($elements);
		//echo "<br>BBBBB<br>";
		$valueMap = array();
		foreach ($elements as $element)
		{
			$single = explode('=', $element);
			$valueMap[$single[0]] = $single[1];
		}
		//print_r($valueMap);
		//echo "<br>CCCCC<br>";
		//exit(0);
		if(null !== $valueMap["sign"])
		{
			$valueMap["sign"] = urldecode($valueMap["sign"]);
		}
		if(null !== $valueMap["extReserved"])
		{
			$valueMap["extReserved"]= urldecode($valueMap["extReserved"]);
		}
		if(null !== $valueMap["sysReserved"])
		{
			$valueMap["sysReserved"] = urldecode($valueMap["sysReserved"]);
		}
		//var_dump($valueMap);
		/*$orderLog->log("huawei", array("result" => $valueMap['result'], "userName" => $valueMap['userName'], 
				"productName" => $valueMap['productName'], "payType" => $valueMap['payType'], "amount" => $valueMap['amount'],
				"orderId" => $valueMap['orderId'],"notifyTime" => $valueMap['notifyTime'], "requestId" => $valueMap['requestId'], "sign" => $valueMap['sign']
		));*/
		
		ksort($valueMap);
		$sign = $valueMap["sign"];
		
		if(empty($sign))
		{
			echo "{\"result\":1}";
			return;
		}

		$content = "";
		$i = 0;
		foreach($valueMap as $key=>$value)
		{
			if($key != "sign" )
			{
				$content .= ($i == 0 ? '' : '&').$key.'='.$value;
			}
			$i++;
		}
		//var_dump($content);
		//$filename = dirname(__FILE__)."/payPublicKey.pem";
		$filename = dirname(__FILE__)."/include/huawei/payPublicKey.pem";
		
		if(!file_exists($filename))
		{
			echo "{\"result\" : 1 }";
			return;
		}
		$pubKey = @file_get_contents($filename);
		$openssl_public_key = @openssl_get_publickey($pubKey);
		//$openssl_public_key = @openssl_pkey_get_public($pubKey);
		$ok = @openssl_verify($content,base64_decode($sign), $openssl_public_key);
		@openssl_free_key($openssl_public_key);
		
		$result = "";
		//print_r($ok);
		//exit(0);
		if($ok)
		{
			//$result = "0";//支付成功处理业务
			if('0'== $valueMap['result'])
			{
				$status = STATUS_SUCCESS;
			}else
			{
				$status = STATUS_FAILD;
			}
			//进行发货
			if(false == $orderLog->recordOrder($status,  $valueMap['amount'], $valueMap['extReserved'], "huawei", $platform,$valueMap['orderId']))
			{
				$result = "1";	//插入失败
				$orderLog->log("huawei",array("order"=>$valueMap['extReserved'],"money"=>$valueMap['amount'],"result"=>$valueMap['result'],"serverresult"=>"faild"));
			
			}else
			{
				//支付成功
				$result = "0";
				$orderLog->log("huawei",array("order"=>$valueMap['extReserved'],"money"=>$valueMap['amount'],"result"=>$valueMap['result'],"serverresult"=>"success"));
			}
		}
		else
		{
			$result = "1";
		}
		$res = "{ \"result\": $result} ";
		echo $res;
		/*require_once('include/wandoujia/rsa.php');	
		//echo "login";
		$orderLog = new ChargeOrder();
		$orderLog->log("huawei", $_POST);
		
		ksort($_POST);
		$sign = $_POST['sign'];
		
		if(empty($sign))
		{
			echo "{\"result\":1}";
			return;
		}
		
		$productName = $_POST['productName'];
		$content = "";
		$i = 0;
		foreach($_POST as $key=>$value)
		{
		   if($key != "sign" )
		    {
			   $content .= ($i == 0 ? '' : '&').$key.'='.$value;
			}
		   $i++;
		}
		$filename = dirname(__FILE__)."/include/huawei/payPublicKey.pem";
		//$filename = "./include/huawei/payPublicKey.pem";
		//echo " filename = $filename ";
		if(!file_exists($filename))
		{
			echo "{\"result\" : 1 }";
			return;
		}
		$pubKey = @file_get_contents($filename);
		//echo "  public key = $pubKey ";
		$openssl_public_key = @openssl_get_publickey($pubKey);
		//echo " openssl public key = $openssl_public_key ";
		$ok = @openssl_verify($content,base64_decode($sign), $openssl_public_key);
		@openssl_free_key($openssl_public_key);
		
		$result = "";
		//echo " ok = $ok ";
		if(1 == $ok)
		{
			$result = "0";//支付成功处理业务
			
			$amount = $_POST['amount'];	//单位是元
			$orderId = $_POST['orderId'];	//华为定单号
			$extrano = $_POST['requestId'];	//额外号
			 
			//进行发货
			if(false == $orderLog->recordOrder(STATUS_SUCCESS,  $amount, $extrano, "huawei", $platform,$orderId))
			{
				$result = "1";	//插入失败
				$orderLog->log("huawei",array("order"=>$extrano,"money"=>$amount,"result"=>$ret,"serverresult"=>"faild"));
				
			}else 
			{
				//支付成功
				$result = "0";
				$orderLog->log("huawei",array("order"=>$extrano,"money"=>$amount,"result"=>$ret,"serverresult"=>"success"));
			}
		}else
		{
			$result = "1";
		}
		$res = "{ \"result\": $result} ";
		echo $res;*/
	}
}